package models

import (
	"gorm.io/gorm"
	"house_srv/house"
)

type House struct {
	gorm.Model
	UserId   int64   `gorm:"type:int;not null;commit:'用户Id'"`
	Title    string  `gorm:"type:varchar(100);not null;commit:'标题'"`
	MinPrice float64 `gorm:"type:decimal(10,2);not null;commit:'最小价格'"`
	MaxPrice float64 `gorm:"type:decimal(10,2);not null;commit:'最大价格'"`
	Price    float64 `gorm:"type:decimal(10,2);not null;commit:'价格'"`
	Address  string  `gorm:"type:varchar(100);not null;commit:'地址'"`
	Image    string  `gorm:"type:varchar(100);not null;commit:'图片'"`
	Size     int64   `gorm:"type:int;not null;commit:'面积'"`
	Staus    int64   `gorm:"type:int;default:1;not null; commit:'用户状态: 1-审核,2-审核通过,3-审核不通过'"`
}

func (h *House) HouseAdd() error {
	return DB.Debug().Create(&h).Error
}
func (h *House) HouseList() error {
	return DB.Debug().Where("user_id", h.UserId).Find(&h).Error
}
func (h *House) HousePrice(Min, Max float64) (houses []*house.HouseList, err error) {

	db := DB.Debug()
	if Max > Min {
		db.Where("price>=?", Max)
	} else {
		db.Where("price<=?", Min)
	}
	db.Order("created_at desc")

	return
}
